package com.chuanke.ckfamily.dao.base;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

public class CkUtil {

    private static final DecimalFormat moneyFormate = new DecimalFormat("0.00");

    private static final DecimalFormat toyuanFormate = new DecimalFormat("0");

    public static final String ZERO_MONEY = "0.00";

    public static String getNowDayStr() {
        return DateFormatUtils.format(new Date(), DateFormatUtils.ISO_DATE_FORMAT.getPattern());
    }

    public static String getYesterdayStr() {
        Calendar date = GregorianCalendar.getInstance();
        date.add(Calendar.DAY_OF_YEAR, -1);

        return DateFormatUtils.format(date, DateFormatUtils.ISO_DATE_FORMAT.getPattern());
    }

    public static String formateToYuan(Long money) {
        if (money == null || money == 0l) {
            return ZERO_MONEY;

        } else {
            return toyuanFormate.format(money/100d);

        }
    }

    public static String formatMoney(Long money) {
        if (money == null || money == 0l) {
            return ZERO_MONEY;

        } else {
            return moneyFormate.format(money/100d);

        }
    }

    public static String formateServerMoney(Double money) {
        if (money == null || money == 0l) {
            return ZERO_MONEY;

        } else {
            return moneyFormate.format(money);

        }
    }

    public static Long sumMoney(Long money1, Long money2) {
        if (money1 == null) {
            money1 = 0l;
        }

        if (money2 == null) {
            money2 = 0l;
        }

        return money1 + money2;
    }

    public static String getRedpacketTitle(String title) {
        if (StringUtils.isEmpty(title)) {
            return "";
        }

        if (title.length() <= 9) {
            return title;
        }

        return title.substring(0, 9) + "...";
    }

}
